﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace CasinoProject
{
	public partial class LoginPage:Page
	{
        public int MyMoney = 0;

        /// <summary>
        /// Konstruktor
        /// </summary>
		public LoginPage()
		{
			this.InitializeComponent();
            InitControls();
			// Insert code required on object creation below this point.
		}
        /// <summary>
        /// Dane użytkownika odczytane z serwera
        /// </summary>
        public Admin.CasinoUser MyUsr = null;

        /// <summary>
        /// BackgroundWorker do logowania
        /// </summary>
        System.ComponentModel.BackgroundWorker BGWlogin = new System.ComponentModel.BackgroundWorker();

        /// <summary>
        /// Inicjuje stronę
        /// </summary>
        void InitControls()
        {
            BGWlogin.DoWork += new System.ComponentModel.DoWorkEventHandler(BGWlogin_DoWork);
            BGWlogin.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(BGWlogin_RunWorkerCompleted);
        }

        /// <summary>
        /// Event do wywoływany po zakończeniu logowania
        /// </summary>
        /// <param name="sender">nie używane</param>
        /// <param name="e">nie używane</param>
        void BGWlogin_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if (MyUsr != null)
            {
                //Uri uri = new Uri("PageMain.xaml", UriKind.Relative);
                UserCredentials.IdUser = MyUsr.user_id;
                this.NavigationService.Navigate(new Window1(MyUsr,MyMoney));
            }
            else
            {
                //MyUsr = new CasinoProject.Admin.CasinoUser();
                //MyUsr.nick = UserCredentials.UserName;
                //MyUsr.user_id = UserCredentials.IdUser;
                //MyUsr.user_name = MyUsr.nick;
                //this.NavigationService.Navigate(new Window1());
                //MessageBox.Show("Login failed");
            }
        }

        /// <summary>
        /// Event obsługujący logowanie
        /// </summary>
        /// <param name="sender">nie używane</param>
        /// <param name="e">nie używane</param>
        void BGWlogin_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Login();
        }

        /// <summary>
        /// Funkcja dokonująca logowania na serwerze
        /// </summary>
        private void Login()
        {
            Admin.AdminClient client = new CasinoProject.Admin.AdminClient();
            try
            {
                client.ClientCredentials.UserName.UserName = UserCredentials.UserName;
                client.ClientCredentials.UserName.Password = UserCredentials.Passowrd;
                MyUsr = client.Login(UserCredentials.UserName, out MyMoney);

            }
            catch (Exception ex)
            {

                MessageBox.Show("Błąd podczas logowania: " + ex.Message);
            }
            finally
            {
                if (client != null && client.State == System.ServiceModel.CommunicationState.Opened)
                    client.Close();
            }
        }

        /// <summary>
        /// event obsługujący kliknięcie na przycisk Login
        /// </summary>
        /// <param name="sender">nie używane</param>
        /// <param name="e">nie używane</param>
        private void butLogin_Click(object sender, RoutedEventArgs e)
        {            
            if (!BGWlogin.IsBusy)
            { 
                UserCredentials.UserName= txtLogin.Text;
                UserCredentials.Passowrd= BoxPassEnter.Password.ToString();                
                BGWlogin.RunWorkerAsync();
                MyUsr = new CasinoProject.Admin.CasinoUser();                
                //this.NavigationService.Navigate(new Window1(MyUsr, MyMoney));
                //this.NavigationService.Navigate(new Window1(new Admin.CasinoUser(), MyMoney));
            }            
        }

        /// <summary>
        /// event obsługujący kliknięcie na przycisk Register
        /// </summary>
        /// <param name="sender">nie używane</param>
        /// <param name="e">nie używane</param>
        private void butCreateProfile_Click(object sender, RoutedEventArgs e)
        {
            //System.Uri resourceLocater = new System.Uri("PageRegister.xaml", System.UriKind.RelativeOrAbsolute);
            //System.Windows.Application.LoadComponent(this, resourceLocater);
            Uri uri = new Uri("PageRegister.xaml", UriKind.Relative);

            // Get the navigation service that was used to 
            // navigate to this page, and navigate to 
            // AnotherPage.xaml
            this.NavigationService.Navigate(uri);
        }

        /// <summary>
        /// Event obsługujący kliknięcie na przycisk Quit
        /// </summary>
        /// <param name="sender">nie używane</param>
        /// <param name="e">nie używane</param>
        private void butQuit_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }
	}
}